﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using FluentFTP.Model;

namespace FluentFTP.Configuration
{
    public static class Settings
    {
        private static UserSettings userSettings;
        private static List<FtpServerInfo> servers;

        public const string UserSettingsPath = "Settings.xml";
        public const string ServersPath = "Servers.xml";

        public static UserSettings UserSettings
        {
            get
            {
                return userSettings ?? (userSettings = loadUserSettings(UserSettingsPath));
            }
        }

        public static List<FtpServerInfo> Servers
        {
            get
            {
                return servers ?? (servers = loadServers(ServersPath));
            }
        }

       

        public static void Reload()
        {
            loadUserSettings(UserSettingsPath);
            loadServers(ServersPath);
        }

        public static void Save()
        {
            //fileExistsCheck(UserSettingsPath);

            XmlSerializer serializerUserSettings = new XmlSerializer(typeof(UserSettings));
            using (StreamWriter writer = new StreamWriter(UserSettingsPath))
            {
                serializerUserSettings.Serialize(writer, userSettings);
                writer.Flush();
            }

            XmlSerializer serializerServers = new XmlSerializer(typeof(List<FtpServerInfo>));
            using (StreamWriter writer = new StreamWriter(ServersPath))
            {
                serializerServers.Serialize(writer, servers);
                writer.Flush();
            }
        }

        private static bool fileExistsCheck(string path)
        {
            if (!File.Exists(path)) return false;
            return true;
        }

        private static UserSettings loadUserSettings(string path)
        {
            //fileExistsCheck(path);
            try
            {
                XmlSerializer serializer = new XmlSerializer(typeof(UserSettings));
                using (var reader = new StreamReader(path))
                {
                    return (UserSettings)serializer.Deserialize(reader);
                }
            }
            catch (Exception)
            {
                return new UserSettings();
            }
        }

        private static List<FtpServerInfo> loadServers(string path)
        {
            //if (!fileExistsCheck(path)) File.Create(path);
            try
            {
                XmlSerializer serializer = new XmlSerializer(typeof(List<FtpServerInfo>));
                using (var reader = new StreamReader(path))
                {
                    return (List<FtpServerInfo>)serializer.Deserialize(reader);
                }
            }
            catch (Exception)
            {
                return new List<FtpServerInfo>();
            }
        }
    }
}
